#include "Segmentation.h"
#include "time.h"

IplImage* Segmentation::K_means(int* means, int& k, MyImage* im)
{
	CvSize size = cvSize(im->width, im->height);
	IplImage* newIm = cvCreateImage(size,im->depth,1);	
	double* Pixels = im->GetPlane()->GetPlaneAdress();
	byte* newPixels = (uchar*)newIm->imageData;
	double* sum = new double[k];
	int* numbers = new int[k];
	double* newK = new double[k];

	for (int i =0; i<k;++i)
	{
		newK[i]=means[i];
	}
	memset(numbers, 0, sizeof(int)*k);
	memset(sum, 0, sizeof(double)*k);

	int sizeP = im->GetPlane()->GetSize();
	int step =0;
	//while (step != 3)
	//{
		memset(numbers, 0, sizeof(int)*k);
		memset(sum, 0, sizeof(double)*k);
		double min = 0;
		byte meanJ = 0;
		byte val = 0;
		for(int i = 0; i < sizeP; ++i)
		{
			min = 10000000;
			meanJ = 0;
			for(int j = 0; j < k; ++j)
			{
				if (fabs(Pixels[i]-(newK[j]/255.))<min)
				{
					min = fabs(Pixels[i]-newK[j]/255.);
					meanJ = j;
				}
				
			}
			sum[meanJ] += Pixels[i];
			++(numbers[meanJ]);
			//if (step == 2)
 			newPixels[i] = meanJ*2; 
		}
		for (int i = 0; i<k; ++i)
		{
			if (numbers[i]!=0)
			{		
				newK[i] = sum[i]/numbers[i];
			}


		}
	//	++step;
	//}
	if (k!=0)
	{	
		delete[] numbers;
		delete[] newK;
		delete[] sum;
	}


	return newIm;
}
IplImage* Segmentation::Graph(MyImage* im)
{
	CvSize size = cvSize(im->width, im->height);
	IplImage* newIm = cvCreateImage(size,im->depth,1);	
	double* Pixels = im->GetPlane()->GetPlaneAdress();
	byte* newPixels = (uchar*)newIm->imageData;
	int sizeP = im->GetPlane()->GetSize();
	double ** Graph = new double *[sizeP];
	for (int i = 0; i<sizeP; ++i )
	{
		Graph[i] = new double[8];
	}
	for (int i = 0; i< sizeP; ++i)
	{
		
	}
	return newIm;

}

IplImage* Segmentation::Histogram(MyImage* im)
{
	CvSize size = cvSize(im->width, im->height);
	IplImage* newIm = cvCreateImage(size,im->depth,1);	
	double* Pixels = im->GetPlane()->GetPlaneAdress();
	byte* newPixels = (uchar*)newIm->imageData;
	return newIm;
}
IplImage* Segmentation::ChangeDeep(IplImage* im, int depth)
{

	IplImage* newIm = cvCreateImage(cvGetSize(im),depth,im->nChannels);
	int size = im->width*im->height;
	for (int i =0; i< size; ++i)
	{
		((UINT32*)newIm->imageData)[i] = ((uchar*)im->imageData)[i];
	}
	return newIm;
}
IplImage* Segmentation::Treshold(IplImage* im, int treshold)
{
	CvSize size = cvSize(im->width, im->height);

	IplImage* newIm = cvCreateImage(size,im->depth,1);	

	char* Pixels = im->imageData;

	byte* newPixels = (uchar*)newIm->imageData;

	int sizep = im->width*im->height;

	int H = im->height;
	int W = im->width;


	for (int i = W; i < sizep - 1-W; ++i)
	{
		//for (int j = 1; j < W-1; ++j)
		//{
			if (abs(Pixels[i]-Pixels[i-1])>treshold)
			{
				newPixels[i] = 255;
				

			}
			else if (abs(Pixels[i]-Pixels[i+1])>treshold)
			{
				newPixels[i] = 255;
				
			}
			else if (abs(Pixels[i]-Pixels[i+1+W])>treshold)
			{
				newPixels[i] = 255;
				
			}
			else if (abs(Pixels[i]-Pixels[i+W])>treshold)
			{
				newPixels[i] = 255;
				
			}
			else if (abs(Pixels[i]-Pixels[i-1+W])>treshold)
			{
				newPixels[i] = 255;
				
			}
			else if (abs(Pixels[i]-Pixels[i+1-W])>treshold)
			{
				newPixels[i] = 255;
				
			}
			else if (abs(Pixels[i]-Pixels[i-W])>treshold)
			{
				newPixels[i] = 255;

			}
			else if (abs(Pixels[i]-Pixels[i-1-W])>treshold)
			{
				newPixels[i] = 255;
		
			}else
			{
				newPixels[i] = 0;
			}

		//}
	}
	return newIm;
}
